package com.example.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConnection {
    private static final String driverName = "com.mysql.cj.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306/bbcforum";
    private static final String username = "root";
    private static final String password = "gy27979551142";
    static{
        try{
            Class.forName(driverName);
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws Exception{
        return DriverManager.getConnection(url,username,password);
    }

    public static void close(ResultSet rs, Statement st,Connection conn){
        try{
            if (rs !=null){
                rs.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            try{
                if (st !=null){
                    st.close();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }finally {
                if (conn !=null){
                    try{
                        conn.close();
                    }catch (SQLException e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
